package flib;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import processing.core.PApplet;
import processing.core.PImage;

public class FileRename extends PApplet {
String pathPSD="/Users/administrator/projekte/Doktor/References/PDF/Das Grundrisswerk scan/plans/";	
String pathVWX="/Users/administrator/projekte/Doktor/References/PDF/Das Grundrisswerk scan/";	
String pngFolder="/Users/administrator/projekte/Doktor/References/PDF/Das Grundrisswerk scan/pngtest/";
public void setup(){

	//rename(folder);
	copyVWX(pathPSD,pathVWX,new File(pathVWX+"DefaultTemplate.vwx"));
	
	//repairPNG(pngFolder);
}
PImage img;
public void repairPNG(String pngFolder){
	File folder = new File(pngFolder);
	File[] listOfFiles = folder.listFiles();
	for (File file : listOfFiles) {
	    if (file.isFile()&& file.getAbsolutePath().endsWith(".png")) {
	    	 img=this.loadImage(file.getAbsolutePath());
	    	 img.loadPixels();
	    	 for (int j=0;j<img.pixels.length;j++){
	    		// float br=brightness(img.pixels[j]);
	    		 float red=red(img.pixels[j]);
	    		 float alpha=alpha(img.pixels[j]);
	    		 if (alpha<100){
	    			 img.pixels[j]=color(255);
	    		 }
	    		 else {
	    			 img.pixels[j]=color(red);
	    		 }
	    	 }
	    	 img.updatePixels();
	    	 img.save(file.getAbsolutePath());
	    }
	}
}

public void copyVWX(String pathPSD,String pathVWX,File tempFile){
	File folder = new File(pathPSD);
	File[] listOfFiles = folder.listFiles();
	for (File file : listOfFiles) {
	    if (file.isFile()) {
	    	String name=file.getName();
	    	name=name.replace(".psd",".vwx");
	    	try {
				copy(tempFile,new File(pathVWX+"vwx/"+name));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	}
}

public void rename(String path){
	File folder = new File(path);
	File[] listOfFiles = folder.listFiles();

	for (File file : listOfFiles) {
	    if (file.isFile()) {
	    	String name=file.getName();
	    	name=name.replace("f","k");
	    	name=name.replace("b","f");
	    	name=name.replace("k","b");
	        File file2 = new File(path+name);
	        if(!file2.exists()) {
	        	 boolean success = file.renameTo(file2);
	        }
	    }
	}
	

    // Rename file (or directory)
   
}
// Copy the source file to target file.
// In case the dst file does not exist, it is created
void copy(File source, File target) throws IOException {

    InputStream in = new FileInputStream(source);
    OutputStream out = new FileOutputStream(target);

    // Copy the bits from instream to outstream
    byte[] buf = new byte[1024];
    int len;

    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    in.close();
    out.close();
}
public void draw(){
	
}
}
